﻿//
//
// using System.Collections.ObjectModel;
// using Avalonia.Controls;
// using CommunityToolkit.Mvvm.ComponentModel;
// using CommunityToolkit.Mvvm.Input;
//
// namespace AvaloniaApplication1.ViewModels;
//
//
//
// public partial class MainWindowViewModel : ViewModelBase
// {
//     public string Greeting { get; } = "Welcomsea撒热风管阿是热镀管  啊色粉 !";
//
//     public MainWindowViewModel() // 构建函数
//     {
//         if (Design.IsDesignMode) // 判断是否处于设计模式
//         {
//           toDoItems =
//           [
//               new ToDoItemViewModel() { Content = "Hello" },
//               new ToDoItemViewModel() { Content = "Avalonia", IsChecked = true }
//           ];
//         }
//     }
//     public ObservableCollection<ToDoItemViewModel> toDoItems { get; } = [];
//
//
//
//
//
//     [ObservableProperty] // 属性通知
//     [NotifyPropertyChangedFor(nameof(addTodoItemCommand))] // 通知属性变化
//     private string? _newItemContent;  // 类似 Typescript 的 ?  可选类型
//     //属性访问器的语法 ,标记命令绑定
//     [RelayCommand (CanExecute = nameof(CanExecuteAddTodoItem))]
//     private void AddTodoItem()
//     {
//         toDoItems.Add(new ToDoItemViewModel() { Content = NewItemContent });
//         NewItemContent = null;
//         }
//
//
//     private bool CanExecuteAddTodoItem()
//     {
//         return !string.IsNullOrWhiteSpace(NewItemContent); //判断是否为空
//     }
//
//
//
//     private void RemoveItem(ToDoItemViewModel item)
//     {
//         toDoItems.Remove(item);
//     }
//
// }
//
//


using System.Collections.ObjectModel;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace AvaloniaApplication1.ViewModels;


public partial class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        // We can use this to add some items for the designer.
        // You can also use a DesignTime-ViewModel
        if (Design.IsDesignMode)
        {
            ToDoItems = new ObservableCollection<ToDoItemViewModel>(new[]
            {
                new ToDoItemViewModel() { Content = "Hello" },
                new ToDoItemViewModel() { Content = "Avalonia", IsChecked = true}
            });
        }
    }


    public ObservableCollection<ToDoItemViewModel> ToDoItems { get; } = new ObservableCollection<ToDoItemViewModel>();


    // -- Adding new Items --

    /// <summary>
    /// This command is used to add a new Item to the List
    /// </summary>
    [RelayCommand (CanExecute = nameof(CanAddItem))]
    private void AddItem()
    {
        // Add a new item to the list
        ToDoItems.Add(new ToDoItemViewModel() {Content = NewItemContent});

        // reset the NewItemContent
        NewItemContent = null;
    }

    /// <summary>
    /// Gets or set the content for new Items to add. If this string is not empty, the AddItemCommand will be enabled automatically
    /// </summary>
    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(AddItemCommand))] // This attribute will invalidate the command each time this property changes
    private string? _newItemContent;

    /// <summary>
    /// Returns if a new Item can be added. We require to have the NewItem some Text
    /// </summary>
    private bool CanAddItem() => !string.IsNullOrWhiteSpace(NewItemContent);

    // -- Removing Items --


    private void RemoveItem(ToDoItemViewModel item)
    {
        // Remove the given item from the list
        ToDoItems.Remove(item);
    }
}